package com.rainng.coursesystem.dao;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rainng.coursesystem.controller.admin.vo.SysGroupReqDTO;
import com.rainng.coursesystem.controller.admin.vo.SysUserQueryReqDTO;
import com.rainng.coursesystem.controller.admin.vo.SysUserResDTO;
import com.rainng.coursesystem.dao.mapper.RoleMapper;
import com.rainng.coursesystem.dao.mapper.UserGroupMapper;
import com.rainng.coursesystem.dao.mapper.UserRoleMapper;
import com.rainng.coursesystem.model.entity.RoleEntity;
import com.rainng.coursesystem.model.entity.UserGroupEntity;
import com.rainng.coursesystem.model.entity.UserRoleEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
@Slf4j
public class UserGroupDAO extends BaseDAO {
    private final UserGroupMapper mapper;

    public UserGroupDAO(UserGroupMapper mapper) {
        this.mapper = mapper;
    }

    public List<String> getDepartmentNameByUserName(String name){

        return mapper.getDepartmentNameByUserName(name);
    };

    public List<String> getDepartmentIdByUserName(String name){

        return mapper.getDepartmentIdByUserName(name);
    };

    public int saveUserGroup(UserGroupEntity entity){
        return mapper.insert(entity);
    }


    public int insert(UserGroupEntity entity) {
        return mapper.insert(entity);
    }

    public int delete(String id) {
        return mapper.deleteById(id);
    }

    public int deleteByIds(List<String> ids){
        return mapper.deleteBatchIds(ids);
    }

    public UserGroupEntity get(String id) {
        return mapper.selectById(id);
    }

    public int update(UserGroupEntity entity) {
        return mapper.updateById(entity);
    }


    public List<UserGroupEntity> list() {
        return mapper.selectList(new LambdaQueryWrapper<>());
    }

    public long count(QueryWrapper<UserGroupEntity> q) {
        return mapper.selectCount(q);
    }

    public List<UserGroupEntity> getAllList(QueryWrapper<UserGroupEntity> q) {
        return mapper.selectList(q);
    }

    public List<UserGroupEntity> showAllGrouplist() {
        return mapper.getAllUserGroup();
    }


    public List<UserGroupEntity> showGrouplist(SysGroupReqDTO vo) {
        Page<UserGroupEntity> pageParam = new Page<>(vo.getPageNum(), vo.getPageSize());
        IPage<UserGroupEntity> pageModel =  mapper.queryGroupPageList(vo,pageParam);
        List<UserGroupEntity> res = pageModel.getRecords();
//        IPage<RoleEntity> pageModel = mapper.queryRolePageList(vo, pageParam);
//        List<RoleEntity> pos = pageModel.getRecords();
//        List<SysUserResDTO> res = new ArrayList<>();
//        if (pos.size() > 0) {
//            for (RoleEntity r : pos) {
//                SysUserResDTO dto = new SysUserResDTO();
//                BeanUtil.copyProperties(r, dto);
//                res.add(dto);
//            }
//        }
        return res;
    }

    public int showGroupCount(SysGroupReqDTO vo) {

        int total =  mapper.queryGroupCount(vo);
        return total;
    }


    public String getGroupIdByUserName(String userName) {
        String groupId = mapper.getGroupIdByUserName(userName);
        return groupId;
    }
}
